package com.sundablog.service.impl.backend.system.upms.permissions;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.sundablog.mapper.AdminPermissionMapper;
import com.sundablog.mapper.AdminRolePermissionMapper;
import com.sundablog.pojo.AdminPermission;
import com.sundablog.pojo.AdminPermissionExample;
import com.sundablog.pojo.AdminRolePermissionExample;
import com.sundablog.pojo.AdminPermissionExample.Criteria;
import com.sundablog.result.LayUiResult;
import com.sundablog.result.ZTreeResult;
import com.sundablog.service.backend.system.upms.filesystem.FilesyStemService;
import com.sundablog.service.backend.system.upms.permissions.PermissionsService;

/**
 * 权限接口实现类
 * 
 * @ClassName: PermissionsServiceImpl
 * @Description:权限接口实现类
 * @author: 哒哒
 * @date: 2018年3月14日 下午7:01:46
 * 
 * @Copyright: 2018 www.sundablog.com Inc. All rights reserved.
 */
@Service
public class PermissionsServiceImpl implements PermissionsService {

	@Autowired
	private AdminPermissionMapper adminPermissionMapper;
	
	@Autowired
	private AdminRolePermissionMapper adminRolePermissionMapper;
	
	/**
	 * 添加一条权限
	 * Title: addPermissions   
	 * Description:添加一条权限    
	 * @param adminPermission
	 * @return   
	 * @see com.sundablog.service.backend.system.upms.permissions.PermissionsService#addPermissions(com.sundablog.pojo.AdminPermission)
	 */
	@Override
	public int addPermissions(AdminPermission adminPermission) {
		//添加根目录默认pid为0
		if (null == adminPermission.getPid()) {
			adminPermission.setPid(0);
		}
		//状态默认正常
		adminPermission.setStatus((byte) 1);
		//设置时间为现在时间
		adminPermission.setCtime(new Date());
		int i = adminPermissionMapper.insertSelective(adminPermission);
		return i;
	}
	
	/**
	 * 修改一条权限
	 * Title: updataPermissions   
	 * Description:修改一条权限
	 * @param adminPermission
	 * @return   
	 * @see com.sundablog.service.backend.system.upms.permissions.PermissionsService#updataPermissions(com.sundablog.pojo.AdminPermission)
	 */
	@Override
	public int updataPermissions(AdminPermission adminPermission) {
		int i = adminPermissionMapper.updateByPrimaryKeySelective(adminPermission);
		return i;
	}
	
	/**
	 * 禁用一条权限
	 * Title: updataDisablePermissionId   
	 * Description:禁用一条权限  
	 * @param permissionId
	 * @return   
	 * @see com.sundablog.service.backend.system.upms.permissions.PermissionsService#updataDisablePermissionId(int)
	 */
	@Override
	public int updataDisablePermissionId(int permissionId) {
		AdminPermission adminPermission = adminPermissionMapper.selectByPrimaryKey(permissionId);
		if (0 == adminPermission.getStatus().intValue()) {
			adminPermission.setStatus((byte) 1);
		} else {
			adminPermission.setStatus((byte) 0);
		}
		int i = adminPermissionMapper.updateByPrimaryKeySelective(adminPermission);
		return i;
	}

	/**
	 * 根据permissionId查询一条数据
	 * Title: updataPermissionsByPermissionId   
	 * Description:    
	 * @param permissionId
	 * @return   
	 * @see com.sundablog.service.backend.system.upms.permissions.PermissionsService#updataPermissionsByPermissionId(int)
	 */
	@Override
	public AdminPermission selectPermissionsByPermissionId(int permissionId) {
		AdminPermission permission = adminPermissionMapper.selectByPrimaryKey(permissionId);
		return permission;
	}
	
	/**
	 * 获取全部权限
	 * Title: findPagingQuery   
	 * Description:    
	 * @return   
	 * @see com.sundablog.service.backend.system.upms.permissions.PermissionsService#findPagingQuery()
	 */
	@Override
	public LayUiResult findPagingQuery() {
		AdminPermissionExample example = new AdminPermissionExample();
		List<AdminPermission> list = adminPermissionMapper.selectByExample(example);
		for (AdminPermission adminPermission : list) {
		}
		
		
		LayUiResult layUiResult = new LayUiResult();
		layUiResult.setCode(0);
		layUiResult.setMsg("");
		layUiResult.setCount(0);
		layUiResult.setData(list);
		return layUiResult;
	}

	/**
	 * 删除一条权限
	 * Title: deletePermission   
	 * Description:    
	 * @param permissionId
	 * @return   
	 * @see com.sundablog.service.backend.system.upms.permissions.PermissionsService#deletePermission(int)
	 */
	@Override
	public int deletePermission(int permissionId) {
		AdminPermissionExample example = new AdminPermissionExample();
		Criteria criteria = example.createCriteria();
		criteria.andPidEqualTo(permissionId);
		List<AdminPermission> list = adminPermissionMapper.selectByExample(example);
		if (list.size() > 0) {
			//删除角色权限关联表的数据
			for (AdminPermission adminPermission : list) {
				AdminRolePermissionExample rolePermissionExample = new AdminRolePermissionExample();
				com.sundablog.pojo.AdminRolePermissionExample.Criteria rolePermissionExampleCriteria = rolePermissionExample.createCriteria();
				rolePermissionExampleCriteria.andPermissionIdEqualTo(adminPermission.getPermissionId());
				adminRolePermissionMapper.deleteByExample(rolePermissionExample);
			}
			//在删除权限表在数据
			AdminPermissionExample permissionExample = new AdminPermissionExample();
			Criteria createCriteria = permissionExample.createCriteria();
			createCriteria.andPidEqualTo(permissionId);
			int i = adminPermissionMapper.deleteByExample(permissionExample);
			return i;
		} else {
			int i = adminPermissionMapper.deleteByPrimaryKey(permissionId);
			return i;
		}
		
		
		
	}
	/**
	 * 查询全部权限返回ztree需要的结构
	 * Title: selectPermissions   
	 * Description:    
	 * @return   
	 * @see com.sundablog.service.backend.system.upms.permissions.PermissionsService#selectPermissions()
	 */
	@Override
	public List<ZTreeResult> selectPermissions() {
		AdminPermissionExample example = new AdminPermissionExample();
		List<AdminPermission> list = adminPermissionMapper.selectByExample(example);
		List<ZTreeResult> zTreeResults = new ArrayList<>();
		for (AdminPermission adminPermission : list) {
			ZTreeResult zTreeResult = new ZTreeResult();
			zTreeResult.setId(adminPermission.getPermissionId());
			zTreeResult.setpId(adminPermission.getPid());
			zTreeResult.setName(adminPermission.getName());
			zTreeResults.add(zTreeResult);
		}
		return zTreeResults;
	}

}
